//
//  YZFasterPopView.m
//  VET
//
//  Created by 刘文靖 on 2018/7/26.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZFasterPopView.h"
#import "UIImage+YZEffects.h"
#import "UIView+YZAnimation.h"
#import "YZFasterPopCustomBtn.h"
#import "UIButton+YZSetImage.h"
#import "YZFasterPopCenterView.h"
#import "YZFasterPopItem.h"
@interface YZFasterPopView ()<YZFasterPopCenterViewDelegate,YZFasterPopCenterViewDataSource>

@property (nonatomic,weak) UIImageView * background;
@property (nonatomic,weak) UIImageView * logo;
@property (nonatomic,weak) UIButton    *closeBtn;
@property (nonatomic,weak) UIButton    *tipButton;
@property (nonatomic,weak) YZFasterPopCenterView * centerView;

@property (nonatomic,strong) NSArray * items;
@property (nonatomic,copy) didSelectItemBlock selectBlock;


@end


@implementation YZFasterPopView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView * iv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:iv];
        self.background = iv;
        UIImageView * logo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"faster_xiaoying"]];
        logo.origin = CGPointMake((kScreenWidth - 72) * 0.5, 102);
        [self addSubview:logo];
        self.logo = logo;
        [self.logo fadeInWithTime:0.25];
        
        UIImageView * triangle = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pop_triangle"]];
        [self addSubview:triangle];
        [triangle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(0);
            make.top.mas_equalTo(self.logo.mas_bottom).offset(5);
        }];
        
        UIButton *tipBtn = [[UIButton alloc] init];
        tipBtn.userInteractionEnabled = NO;
        [tipBtn setTitle:@"小影为您服务，想记录点什么？" forState:UIControlStateNormal];
        tipBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
        [tipBtn setTitleColor:kColorWithHex(0x333333) forState:UIControlStateNormal];
        tipBtn.backgroundColor = kColorWithHex(0xE9EAEB);
        tipBtn.layer.cornerRadius = 22;
        [self addSubview:tipBtn];
        [tipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(33);
            make.right.offset(-33);
            make.height.mas_equalTo(44);
            make.top.mas_equalTo(triangle.mas_bottom);
        }];
        
        self.tipButton = tipBtn;
        UIButton *closeBtn = [[UIButton alloc] init];
        [closeBtn setBackgroundImage:[UIImage imageNamed:@"faster_close"] forState:UIControlStateNormal];
        [self addSubview:closeBtn];
        [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.offset(-5 - YZ_BOTTOM_MARGIN);
            make.centerX.offset(0);
            make.size.mas_equalTo(CGSizeMake(42, 42));
        }];
        self.closeBtn = closeBtn;
        [self.closeBtn revolvingWithTime:.25 andDelta:(M_PI_2)];
        [self.closeBtn fadeInWithTime:0.25];
        @weakify(self)
        [[self.closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            [self hideItems];
            [self hide];
        }];
        
        YZFasterPopCenterView * centerView = [[YZFasterPopCenterView alloc]initWithFrame:CGRectMake(0, self.frame.size.height * 0.37, self.frame.size.width, self.frame.size.height * 0.4)];
        [self addSubview:centerView];
        centerView.delegate = self;
        centerView.dataSource = self;
        centerView.clipsToBounds = NO;
        self.centerView = centerView;
        
    }
    return self;
}

- (void)removeitemsComplete{
    self.superview.userInteractionEnabled = YES;
}


- (void)showItems{
    [self.centerView reloadData];
}

- (void)hideItems{
    [self.logo fadeOutWithTime:.25];
    [self.closeBtn revolvingWithTime:.25 andDelta:0];
    [self.closeBtn fadeOutWithTime:.25];
    self.tipButton.alpha = 0;
    [self.centerView dismis];
}

+ (instancetype)showToView:(UIView *)view withItems:(NSArray *)array andSelectBlock:(didSelectItemBlock)block{
    [self viewNotEmpty:view];
    YZFasterPopView * popView = [[YZFasterPopView alloc]initWithFrame:view.bounds];
    popView.background.image = [self imageWithView:view];
    [view addSubview:popView];
    popView.selectBlock = block;
    [popView fadeInWithTime:0.25];
    popView.items = array;
    [popView showItems];
    return popView;
}


+ (instancetype)showToView:(UIView *)view andImages:(NSArray *)imageArray andTitles:(NSArray *)titles andSelectBlock:(didSelectItemBlock)block{
    NSUInteger count = imageArray.count;
    NSMutableArray * items = [NSMutableArray array];
    for (int i = 0; i < count; i++) {
        YZFasterPopItem * item = [[YZFasterPopItem alloc]initWithTitle:titles[i] icon:imageArray[i]];
        [items addObject:item];
    }
    return [self showToView:view withItems:items andSelectBlock:block];
}

+ (UIImage *)imageWithView:(UIView *)view{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.frame.size.width, view.frame.size.height), NO, [[UIScreen mainScreen] scale]);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIColor *tintColor = [UIColor colorWithWhite:0.95 alpha:0.78];
    image = [image yz_applyBlurWithRadius:15 tintColor:tintColor saturationDeltaFactor:1 maskImage:nil];
    UIGraphicsEndImageContext();
    
    return image;
}

+ (void)viewNotEmpty:(UIView *)view{
    if (view == nil) {
        view = (UIView *)[[UIApplication sharedApplication] delegate];
    }
    
}

+ (void)hideFromView:(UIView *)view{
    [self viewNotEmpty:view];
    [view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIView * subV = (UIView *)obj;
        [subV isKindOfClass:[self class]];
        [YZFasterPopView hideWithView:subV];
    }];
}

- (void)hide{
    [YZFasterPopView hideWithView:self];
}

+ (void)hideWithView:(UIView *)view{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [view fadeOutWithTime:0.35];
    });
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self hideItems];
    [self hide];
}


#pragma mark centerview delegate and datasource
- (NSInteger)numberOfItemsWithCenterView:(YZFasterPopCenterView *)centerView
{
    return self.items.count;
}

-(YZFasterPopItem *)itemWithCenterView:(YZFasterPopCenterView *)centerView item:(NSInteger)item
{
    return self.items[item];
}

-(void)didSelectItemWithCenterView:(YZFasterPopCenterView *)centerView andItem:(YZFasterPopItem *)item
{
    if (self.selectBlock) {
        self.selectBlock(item);
    }
    if (item.autoHide) {
        [self hide];
    }
}

- (void)didSelectMoreWithCenterView:(YZFasterPopCenterView *)centerView andItem:(YZFasterPopGroup *)group
{
    if (self.selectBlock) {
        self.selectBlock(group);
    }
}

@end
